﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Entities;
namespace Data
{
   public class BaoCaoCongNoRepository
    {
       public static bool Exists(DateTime thang)
       {
           NhaSachEntities context = new NhaSachEntities();

           var q = (from bc in context.BaoCaoCongNoList
                    where ((DateTime)bc.Thang).Month == thang.Month && ((DateTime)bc.Thang).Year == thang.Year
                    select bc).Count();
           if (q > 0)
               return true;
           return false;

       }
       public static BaoCaoCongNoDTO GetBaoCaoCongNoByThang(DateTime thang)
       {
           NhaSachEntities context = new NhaSachEntities();

           var q = (from bc in context.BaoCaoCongNoList
                    where ((DateTime)bc.Thang).Month == thang.Month && ((DateTime)bc.Thang).Year == thang.Year
                    select new BaoCaoCongNoDTO()
                    {
                        MaBaoCaoCongNo = bc.MaBaoCaoCongNo,
                        Thang = (DateTime)bc.Thang,
                        TinhTrang = (int)bc.TinhTrang
                    }).FirstOrDefault();
           return q;
       }
       public static int GetCurrentID()
       {
           NhaSachEntities context = new NhaSachEntities();
           Nullable<int> id = context.GetCurrentID("BaoCaoCongNo").FirstOrDefault();
           return (int)id;
       }

       public static void Insert(BaoCaoCongNoDTO BaoCaoCongNo)
       {
           NhaSachEntities context = new NhaSachEntities();
           BaoCaoCongNo bc = new BaoCaoCongNo()
           {
               Thang = BaoCaoCongNo.Thang,
               TinhTrang = BaoCaoCongNo.TinhTrang
           };
           context.AddToBaoCaoCongNoList(bc);
           context.SaveChanges();
       }
       
    }
}
